<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线点名系统 - 教师查看记录</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fa;
            color: #333;
            padding: 20px;
        }
        h1 {
            text-align: center;
            margin-bottom: 20px;
            font-size: 24px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            overflow: hidden;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: center;
        }
        th {
            background-color: #28a745;
            color: white;
        }
        tr:hover {
            background-color: #f1f1f1;
        }
    </style>
</head>
<body>
<h1>签到记录</h1>
<table id="attendanceTable">
    <tr>
        <th>姓名</th>
        <th>是否出席</th>
        <th>签到时间</th>
    </tr>
</table>

<script>
    // 获取签到记录并显示在表格中
    fetch('/api/students/attendance')
        .then(response => {
            if (!response.ok) {
                throw new Error('无法获取签到记录');
            }
            return response.json();
        })
        .then(data => {
            const table = document.getElementById('attendanceTable');
            data.forEach(student => {
                const row = table.insertRow();
                row.insertCell(0).innerText = student.name;
                row.insertCell(1).innerText = student.present ? '是' : '否';
                row.insertCell(2).innerText = student.present ? formatTimestamp(student.attendanceTime) : '未签到';
            });
        })
        .catch(error => {
            const table = document.getElementById('attendanceTable');
            const row = table.insertRow();
            const cell = row.insertCell(0);
            cell.colSpan = 3; // 合并单元格
            cell.innerText = error.message;
            cell.style.color = 'red';
            cell.style.textAlign = 'center';
        });

    // 格式化时间戳为可读格式
    function formatTimestamp(timestamp) {
        const date = new Date(timestamp);
        return date.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit',
            hour12: false
        });
    }
</script>
</body>
</html>